/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package coordinates;

/**
 *
 * @author Home
 */
public class NoiseRemover {

    int[] disX = new int[20];
    int[] posX = new int[20];
    int[] disY = new int[20];
    int[] posY = new int[20];
    double[] weights = {1.4, 1.3, 1.2, 1.1, 1, 0.9, 0.8, 0.7, 0.6};

    public int[] smoothing1(int[][] points) {
        int[] nextpoint = new int[2];
        int[] dx = new int[points.length - 1];
        int[] dy = new int[points.length - 1];


        double[] tanAngle = new double[points.length - 1];
        for (int i = 0; i < points.length - 1; i++) {
            dx[i] = points[i + 1][0] - points[i][0];
            dy[i] = points[i + 1][1] - points[i][1];
            // tanAngle[i] = dy[i] / dx[i];
        }

        double angleMean = 0;
        double dxMean = 0;
        double dyMean = 0;
        for (int i = 0; i < points.length - 2; i++) {
            dxMean = dxMean + dx[i];
            dyMean = dyMean + dy[i];
            angleMean = angleMean + tanAngle[i];
        }
        dxMean = dxMean / (points.length - 2);
        dyMean = dyMean / (points.length - 2);
        angleMean = angleMean / (points.length - 2);
        System.out.println("dxMean=" + dxMean + " dyMean=" + dyMean + " angleMean=" + angleMean);
        boolean dxV = dx[points.length - 2] > 5 * dxMean;
        boolean dyV = dy[points.length - 2] > 5 * dyMean;
        if (dxV) {
            nextpoint[0] = (int) (points[points.length - 2][0] + dxMean);
        } else {
            nextpoint[0] = (int) (points[points.length - 1][0]);
        }
        if (dyV) {
            nextpoint[1] = (int) (points[points.length - 2][1] + dyMean);
        } else {
            nextpoint[1] = (int) (points[points.length - 1][1]);
        }

        System.out.println("dxV=" + dxV + " dyV=" + dyV);
        return nextpoint;
    }

    public static void main(String[] args) {
        int[][] points = new int[10][2];
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 2; j++) {
                points[i][j] = 2 * i;
            }
        }
        points[9][0] = 100;
        points[9][1] = 200;
        Smoother sm = new Smoother();
        int[] p = sm.smoothing1(points);
        System.out.println("x=" + p[0] + " y=" + p[1]);

    }

    public int calculateX(int begin) {
        double tot=0;
        for (int io = 0; io < weights.length; io++) {
            tot=tot+weights[io]*disX[(begin+io)%weights.length];
        }
        tot=tot/weights.length;
        return (int)tot;
    }

    public int calculateY(int begin) {
        double tot=0;
        for (int io = 0; io < weights.length; io++) {
            tot=tot+weights[io]*disY[(begin+io)%weights.length];
        }
        tot=tot/weights.length;
        return (int)tot;
    }
}
